package com.leoman.comment.service.impl;

import com.leoman.comment.service.WorkCommentService;
import com.leoman.common.controller.common.CommonController;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.work.dao.WorkCommentDao;
import com.leoman.work.entity.WorkComment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/8/26 0026.
 *
 */
@Service
public class WorkCommentServiceImpl extends GenericManagerImpl<WorkComment, WorkCommentDao> implements WorkCommentService {

    @Autowired
    private WorkCommentDao workCommentDao;

    @Override
    public Page<WorkComment> pageList(final String name, final String mobile, final Integer type, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        Page<WorkComment> page = workCommentDao.findAll(new Specification<WorkComment>() {
            @Override
            public Predicate toPredicate(Root<WorkComment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (StringUtils.isNotEmpty(name)) {
                    Predicate pre = cb.like(root.get("name").as(String.class), "%" + name + "%");
                    predicateList.add(pre);
                }

                if (type != null) {
                    Predicate pre = cb.equal(root.get("type").as(Integer.class), type);
                    predicateList.add(pre);
                }

                if (StringUtils.isNotEmpty(mobile)) {
                    Predicate pre = cb.like(root.get("userInfo").get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }

                /*Predicate pre = cb.equal(root.get("typePlus").as(Integer.class), 0);
                predicateList.add(pre);*/

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);

        return page;
    }

    @Override
    public List<WorkComment> findListNew(Integer type) {
        return workCommentDao.findListNew(CommonController.getOldDate(), type);
    }

    @Override
    @Transactional
    public void deleteComment(Long commentId) {

        if (commentId != null) {
            // 先删除评论的回复
            List<WorkComment> replyList = workCommentDao.findListByCommentId(commentId);
            for (WorkComment workComment : replyList) {
                delete(workComment);
            }

            // 后删除评论
            WorkComment workComment = workCommentDao.findOne(commentId);
            delete(workComment);
        }
    }

    @Override
    public List<WorkComment> findList(Long workId, Integer type, Integer status, Integer isDetail) {
        return workCommentDao.iFindList(workId, type, status, isDetail);
    }
}
